package com.jiangyg.mall.authz.support.authentication.member;

import com.jiangyg.mall.authz.constant.AuthzConstant;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 类描述：门户网站（会员）退出登录处理器
 *
 * @author jiangyg
 * @version 4.0
 * @date 2021-10-09
 */
@Slf4j
public class MemberLogoutHandlerImpl implements LogoutHandler {

    public MemberLogoutHandlerImpl() {
    }

    /**
     * 功能描述：根据请求中的token信息，清除安全认证的缓存
     *
     * @param request        请求
     * @param response       响应
     * @param authentication 认证
     */
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        // 移除会话中的用户信息
        final HttpSession session = request.getSession();
        session.removeAttribute(AuthzConstant.MEMBER_INFO_SESSION_ID);
        // 返回成功提示
        WebUtils.out(response, Result.ok());
    }

}
